#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_pBtnGp = new QButtonGroup(this);
    m_pBtnGp->setExclusive(true);

    QAction *pActionH = ui->mainToolBar->addAction("水平分屏");
    connect(pActionH,&QAction::triggered,this,[=]{
        qDebug()<<"水平分屏";
        QPushButton *pBtn = createBtn(Qt::Horizontal);
    });

    QAction *pActionV = ui->mainToolBar->addAction("垂直分屏");
    connect(pActionV,&QAction::triggered,this,[=]{
        qDebug()<<"垂直分屏";
        QPushButton *pBtn = createBtn(Qt::Vertical);


    });

    QAction *pActionDel = ui->mainToolBar->addAction("删除分屏");
    connect(pActionDel,&QAction::triggered,[=]{
        qDebug()<<"删除分屏";
//        ui->centralWidget->removeWidget(m_pBtnGp->checkedButton());
//        ui->centralWidget->removeWidget2(m_pBtnGp->checkedButton());
        ui->centralWidget->removeWidget3(m_pBtnGp->checkedButton());
        if(!m_pBtnGp->buttons().isEmpty())
            m_pBtnGp->buttons().last()->setChecked(true);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

QPushButton *MainWindow::createBtn(Qt::Orientation orientation)
{
    QPushButton *pBtn = new QPushButton(this);
    pBtn->setText(QString("Btn%1").arg(m_pBtnGp->buttons().count()));
    pBtn->setObjectName(pBtn->text());
    pBtn->setCheckable(true);
//    pBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    pBtn->setBaseSize(0,0);
//    pBtn->setStyleSheet("QPushButton{padding:5px;border-radius:5px; background:white;border: 1px solid white;}"
//                        "QPushButton:hover{border-color:#f2f2f2;}"
//                        "QPushButton:pressed{border-color:#e6e6e6;}"
//                        "QPushButton:checked{border-color:#33ffd6;}");
    connect(pBtn,&QObject::destroyed,[=]{
        qDebug()<<"按钮释放:"<<pBtn;
    });
    m_pBtnGp->addButton(pBtn);
    //往窗口分割器中添加一个控件
//    ui->centralWidget->addWidget(m_pBtnGp->checkedButton(),pBtn,orientation);
//    ui->centralWidget->addWidget2(m_pBtnGp->checkedButton(),pBtn,orientation);
    ui->centralWidget->addWidget3(m_pBtnGp->checkedButton(),pBtn,orientation);


    pBtn->setChecked(true);
    return pBtn;
}
